<!-- 实际呈现效果的组件 -->
<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      myChart: null,
      allData: null,
      startValue: 0, // 区域缩放的起始值
      endValue: 9, // 区域缩放的终点值
      timerNum: null,
    };
  },

  created() {
    this.$socket.registerCallBack("rankType", this.getData);
  },
  mounted() {
    this.initChart();
    this.getData();

    // this.$socket.send({
    //   action: "getData",
    //   socketType: "rankType",
    //   chartName: "rank",
    //   value: "",
    // });

    window.addEventListener("resize", this.screenAdapter);
    this.startInterval();
    this.screenAdapter();
  },
  destroyed() {
    this.$socket.unregisterCallBack("rankType");
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerNum);
  },

  methods: {
    //初始化echarts实例对象,要在dom结果加载完成后调用
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.rank_ref, "chalk");
      const initOption = {
        title: {
          text: "区域供应商评分",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        toolbox: {
          show: false,
          showTitle: true,
          feature: {
            saveAsImage: {},
            restore: {},
            dataView: {},
            dataZoom: {
              show: true,
            },
            magicType: {
              type: ["line", "bar"],
            },
          },
        },
        legend: {
          show: true,
        },
        tooltip: {
          show: true,
          trigger: "axis",
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: {
          type: "bar",
        },
      };
      this.myChart.setOption(initOption);
      this.myChart.on("mouseout", this.startInterval);
      this.myChart.on("mouseover", () => {
        clearInterval(this.timerNum);
      });
    },
    // 获取数据
    async getData() {
      // await this.$http.get()
      const { data: ret } = await this.$http.get("rank");
      // console.log(ret);

      ret.sort((a, b) => {
        return -a.value + b.value;
      });

      // 对allData进行赋值
      this.allData = ret;

      //更新图表
      this.updateChart();
    },

    // 更新图表
    updateChart() {
      // 处理图表需要的数据
      // 所有省份名的数组
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });

      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                if (arg.value > 300) {
                  return "red";
                } else if (arg.value < 200) {
                  return "green";
                } else {
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: "red",
                    },
                    {
                      offset: 1,
                      color: "green",
                    },
                  ]);
                }
              },
            },
          },
        ],
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false, // 是否显示拖动条
          startValue: this.startValue,
          endValue: this.endValue,
        },
      };
      this.myChart.setOption(dataOption);
    },

    startInterval() {
      this.timerNum = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        xAxis: {
          axisLabel: {
            fontSize: titleFontSize / 2,
          },
        },
        yAxis: {
          axisLabel: {
            fontSize: titleFontSize / 2,
          },
        },
        series: {
          barWidth: titleFontSize,
          itemStyle: {
            barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
          },
        },
      };

      this.myChart.setOption(adapterOption);

      // 收到调用resize才能产生效果
      this.myChart.resize();
    },
  },
};
</script>
<style lang=""></style>
